package com.yenlien.traceyun.webase.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BusinessException extends RuntimeException {

    private Logger logger = LoggerFactory.getLogger(BusinessException.class);

    private String code;

    private String msg;

    public BusinessException(String msg) {
        this.code = "500";
        this.msg = msg;
        logger.error(" error code:{},msg:{}", "500", msg);
    }

    public BusinessException(String code, String msg) {
        this.code = code;
        this.msg = msg;
        logger.error(" error code:{},msg:{}", code, msg);
    }

    public BusinessException() {
        super();
    }

    public BusinessException(Throwable throwable) {
        super(throwable);
        this.msg = throwable.getMessage();
        log(null, throwable);
    }

    public BusinessException(String code, Throwable throwable) {
        super(throwable);
        log(code, throwable);
    }

    private void log(String code, Throwable throwable) {
        if (throwable != null) {
            StackTraceElement element = throwable.getStackTrace()[0];
            String exceptionInfo = null;
            if (element == null)
                return;
            exceptionInfo = "class=" + element.getClassName() + ",method=" + element.getMethodName() + ",file="
                    + element.getFileName() + ",line=" + element.getLineNumber();
            logger.error("error code:{},exception msg:{}", code, exceptionInfo);
        }
        printStackTrace();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
